<template>
  <div class="app-container">
    <el-card class="queryForm">
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item :label="$t('partner.name')" prop="name">
          <el-input
              v-model="queryParams.name"
              :placeholder="$t('partner.namePlaceholder')"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item :label="$t('partner.code')" prop="code">
          <el-input
              v-model="queryParams.code"
              :placeholder="$t('partner.codePlaceholder')"
              clearable
              style="width: 200px"
              @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item :label="$t('partner.status')" prop="status">
          <el-select
              v-model="queryParams.status"
              :placeholder="$t('partner.statusPlaceholder')"
              clearable
              style="width: 200px"
          >
            <el-option
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">{{ $t('jbx.text.query') }}</el-button>
          <el-button icon="Refresh" @click="resetQuery">{{ $t('jbx.text.reset') }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="common-card">
      <div class="btn-form">
        <el-button type="primary" @click="handleAdd">{{ $t('jbx.text.add') }}</el-button>
        <el-button type="danger" :disabled="multiple" @click="handleDeleteBatch">{{ $t('jbx.text.delete') }}</el-button>
      </div>
      <el-table v-loading="loading" :data="partnerList" border @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column :label="$t('partner.id')" align="center" prop="id" />
        <el-table-column :label="$t('partner.name')" align="center" prop="name" />
        <el-table-column :label="$t('partner.code')" align="center" prop="code" width="120" />
        <el-table-column :label="$t('partner.phone')" align="center" prop="phone" width="120" />
        <el-table-column :label="$t('partner.partnerType')" align="center" prop="partnerType" />
        <el-table-column :label="$t('partner.status')" align="center" prop="status" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? $t('jbx.text.enable') : $t('jbx.text.disable') }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column :label="$t('jbx.text.createdDate')" align="center" prop="createdDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createdDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('jbx.text.action')" align="center" class-name="small-padding fixed-width" width="140">
          <template #default="scope">
            <el-button link icon="View" @click="handleView(scope.row)"></el-button>
            <el-button link icon="Edit" @click="handleUpdate(scope.row)"></el-button>
            <el-button link icon="Delete" @click="handleDelete(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改合作方对话框 -->
    <edit-form
        :title="title"
        :open="open"
        :form-id="id"
        @dialogOfClosedMethods="dialogOfClosedMethods"
    ></edit-form>
    
    <!-- 合作方详情抽屉 -->
    <partner-detail 
      :title="detailTitle" 
      :open="detailOpen" 
      :form-id="detailId"
      @update:open="onDetailClose"
    />
  </div>
</template>

<script setup name="Partner" lang="ts">
import { ref, getCurrentInstance, reactive, toRefs, nextTick } from "vue";
import { ElForm } from "element-plus";
import modal from "@/plugins/modal";
import { useI18n } from "vue-i18n";
import { parseTime } from "@/utils/Jinbooks";
import { listPartner, deletePartner, deleteBatchPartner } from "@/api/channel/partner";
import EditForm from './edit.vue'
import PartnerDetail from './detail.vue'

const { t } = useI18n();
const { proxy } = getCurrentInstance()!;

// 字典数据
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

// 定义变量
const partnerList = ref([]);
const loading = ref(true);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const open = ref(false);
const ids = ref([]);
const id = ref<string | undefined>()

// 合作方详情相关变量
const detailOpen = ref(false)
const detailTitle = ref('')
const detailId = ref('')

// 查询参数
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    code: undefined,
    phone: undefined,
    status: undefined
  }
});

const { queryParams } = toRefs(data);
const queryRef = ref<InstanceType<typeof ElForm> | null>(null);

// 获取列表
function getList() {
  loading.value = true;
  listPartner(queryParams.value).then((response: any) => {
    partnerList.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
    partnerList.value = [];
    total.value = 0;
  });
}

// 查询
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

// 重置
function resetQuery() {
  queryRef.value?.resetFields();
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

// 新增
function handleAdd() {
  id.value = undefined
  title.value = t('partner.add');
  open.value = true;
}

// 修改
function handleUpdate(row) {
  const partnerId = row.id || ids.value[0];
  id.value = partnerId
  title.value = t('partner.edit');
  open.value = true;
}

// 查看详情
function handleView(row) {
  detailId.value = row.id;
  detailTitle.value = t('partner.detail');
  detailOpen.value = true;
}

// 删除单个
function handleDelete(row) {
  const partnerId = row.id;
  modal.confirm(t('jbx.confirm.text.delete')).then(function() {
    return deletePartner(partnerId);
  }).then(() => {
    modal.msgSuccess(t('jbx.alert.delete.success'));
    getList();
  }).catch(() => {});
}

// 批量删除
function handleDeleteBatch() {
  modal.confirm(t('jbx.confirm.text.delete')).then(function() {
    return deleteBatchPartner(ids.value);
  }).then(() => {
    modal.msgSuccess(t('jbx.alert.delete.success'));
    getList();
  }).catch(() => {});
}

// 详情关闭回调
function onDetailClose() {
  detailOpen.value = false;
}

// 对话框关闭回调
function dialogOfClosedMethods(val) {
  open.value = false;
  if (val) {
    getList();
  }
}

// 初始化数据
getList();
</script>

<style scoped>
.common-card {
  margin-bottom: 15px;
}
</style>